/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elementsDefense.towers.projectils;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Spline;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.scene.shape.Curve;
import com.jme3.scene.shape.Sphere;
import elementsDefense.enemies.BasicEnemy;
import elementsDefense.towers.Tower;

/**
 *
 * @author Ricardo
 */
public class Thunder extends Projectil{
        
    Geometry thunder1,thunder2,thunderBall;
    Material matBlue;
    ThunderControl control; 

    public Thunder(AssetManager assetManager, Node node, Tower source) {
        this.source = source;
        this.assetManager = assetManager;
        this.node =node;
        this.damage = 70;
        matBlue = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        matBlue.setTexture("ColorMap", assetManager.loadTexture("Textures/static.jpg"));
    }
    
    public void shootAt(BasicEnemy target){
        this.target = target;
        
        thunderBall=new Geometry("staticBall", new Sphere(5, 5, 0.3f));
        thunderBall.setMaterial(matBlue);
        thunderBall.setLocalRotation(new Quaternion(new float[]{FastMath.HALF_PI,0,0}));
        thunderBall.setLocalTranslation(target.getLocation());
        node.attachChild(thunderBall);
        
        control=new ThunderControl();
        thunderBall.addControl(control);
    }
    
    public void hit(){
        target.hit(this);
    }
    
    
    
    
    
private class ThunderControl extends AbstractControl{
   float passed=0;
   int state=0;
    @Override
    protected void controlUpdate(float tpf) {
        passed+=tpf;
        thunderBall.setLocalTranslation(target.getLocation());
        if(passed>0.05f){
            switch(state){
                case 0:{
                    thunderBall.setCullHint(Spatial.CullHint.Always);
                    Vector3f[] vs=createThunder();

                    Spline s=new Spline(Spline.SplineType.Linear, vs, 0, false);
                    Curve curve=new Curve(s, 1);
                    curve.setLineWidth(4);
                    thunder1=new Geometry("thunder", curve);
                    thunder1.setMaterial(matBlue);
                    node.attachChild(thunder1);
                    break;}
                case 1:
                    thunderBall.setCullHint(Spatial.CullHint.Inherit);
                    thunder1.setCullHint(Spatial.CullHint.Always);
                    Vector3f[] vs=createThunder();

                    Spline s=new Spline(Spline.SplineType.Linear, vs, 0, false);
                    Curve curve=new Curve(s, 1);
                    curve.setLineWidth(4);
                    thunder2=new Geometry("thunder", curve);
                    thunder2.setMaterial(matBlue);
                    node.attachChild(thunder2);
                    break;
                case 2:
                    thunderBall.setCullHint(Spatial.CullHint.Always);
                    thunder1.setCullHint(Spatial.CullHint.Inherit);
                    thunder2.setCullHint(Spatial.CullHint.Always);
                    break;
                case 3:
                    thunderBall.setCullHint(Spatial.CullHint.Inherit);
                    thunder1.setCullHint(Spatial.CullHint.Inherit);
                    thunder2.setCullHint(Spatial.CullHint.Inherit);
                case 4:
                    this.setEnabled(false);
                    thunder1.removeFromParent();
                    thunder1=null;
                    thunder2.removeFromParent();
                    thunder2=null;
                    thunderBall.removeControl(this);
                    thunderBall.removeFromParent();
                    thunderBall=null;
                    hit();
                    return;
            }
            state++;
            passed=0;
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

        private Vector3f[] createThunder() {
            Vector3f[] vs=new Vector3f[5];
            for (int i = 0; i < vs.length; i++) {
                vs[i]=new Vector3f().interpolate(source.fireLocation, target.getLocation(), (float)i/(vs.length-1));
                float rnd1=(float) (Math.random()-0.5)/2f;
                float rnd2=(float) (Math.random()-0.5)/2f;
                vs[i].addLocal(rnd1, rnd2, rnd1+rnd2);
            }
//            vs[0].set(source.fireLocation);
//            vs[vs.length-1].set(target.getLocation());
            return vs;
        }

   
    
}

}
